// pages/home/listen.js
var context = null; //一个音频上下文
const db = wx.cloud.database()
const p_db = db.collection('publish')
const login_db = db.collection('login_users')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    like: true,
    like_list: [],
    flag: true,
    yes:'yes',
    
    audio: {
      isPlaying: true, //播放状态
      //时间位置
      currentTime: 0,
      duration: 0,
      isWarting: false
    }
  },
  play() {
    this.setData({
      flag: true,
      'audio.isPlaying': this.data.audio.isPlaying
    })
    context.play();
    console.log('xxx')
  },
  noplay() {
    this.setData({
      flag: false,
      'audio.isPlaying': !this.data.audio.isPlaying
    })
    context.pause();
    console.log('xxx')
  },

  funPlay: function () {
    console.log("audio play");
  },
  funPause: function () {
    console.log("audio pause");
  },
  time(value) {
    let theTime = value;//秒
    let middle = 0;//分
    // let hour = 0;//小时
    if (theTime > 59) {
      middle = parseInt(theTime / 60);
      theTime = parseInt(theTime % 60);
    }
    // if (middle > 59) {
    //   hour = parseInt(middle / 60);
    //   middle = parseInt(middle % 60);
    // }
    theTime < 10 ? theTime = '0' + theTime : theTime = theTime
    middle < 10 ? middle = '0' + middle : middle = middle
    // hour < 10 ? hour = '0' + hour : hour = hour

    return middle + ':' + theTime

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    p_db
    .get()
    .then(res => {
        this.setData({
            yes: res.data[0].yes
        })
    })
    
    var _this = this;
    var detail = wx.getStorageSync('detail')
    var like_list = wx.getStorageSync('like')
    var test = wx.getStorageSync('like')
    var openid = wx.getStorageSync('openid')
    _this.setData({
      openid: openid,
      test: test,
      id: detail._id,
      like_list: like_list,
      detail: detail,
      url: detail.img,
      name: detail.name,
      text: detail.text,
      audio: detail.audio,
      brief: detail.brief,
    })
    console.log(detail)
    
    
    context = wx.createInnerAudioContext();
    var url=_this.data.audio
    console.log(url)
    context.src = encodeURI(url)

    //开始播放
      context.play()
 

    //播放进行中
    context.onTimeUpdate(() => {
      console.log(context)
      console.info(context.currentTime, context.duration);
      _this.setData({
        'audio.currentTime': _this.time(Math.round(context.currentTime)),
        'audio.duration': _this.time(Math.round(context.duration)),
        'audio.percent': context.currentTime / context.duration * 100,
      });
      // console.log(_this.time(Math.round (context.currentTime)))
    });
    // context.onWaiting(() => {
    //   context.pause(); //当没有加载时候暂停
    // });
    // context.onCanplay(() => {
    //   if (_this.data.audio.isPlaying) {
    //     context.play();//当加载完毕后再播放
    //   }
    // });
    
  },
  
    
  onShow: function () {
    context.autoplay = true;
  },
  like() {
    if(!this.data.openid){
      wx.showModal({
        title: '暂未登录',
        content: '是否需要跳转至登陆页',
        success (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../../my/my',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    }else{
    if (this.data.like == true) {
      this.data.like_list[3].push(this.data.detail)
      console.log(this.data.like_list)
      wx.setStorageSync('like', this.data.like_list)
      this.setData({
        like: false,
      })
    } else {
      for (var i = 0; i < this.data.like_list[3].length; i++) {
        if (this.data.id == this.data.like_list[3][i]._id) {
          console.log(i)
          let index = i
          this.data.like_list[3].splice(index, 1)
          wx.setStorageSync('like', this.data.like_list)
        }
      }
      this.setData({
        like: true
      })
    }}
  },

  /**
       * 生命周期函数--监听页面隐藏
       */
  onHide: function () {
    //  context.stop();
    // context.pause();
  },
  onUnload() {
    if (this.data.like_list != this.data.test) {
      login_db.where({
        '_openid': this.data.openid
      }).update({
        data: {
          like: this.data.like_list,
        }
      }).then(res => {
        console.log('更新成功')
      }).catch(err => {
        console.log('更新失败', err)//失败提示错误信息
      })

    }
  context.destroy();
  },
})